//
//  JSMNewSaveViewController.m
//  TakeYouTourTogether
//
//  Created by dllo on 15/12/5.
//  Copyright © 2015年 king. All rights reserved.
//

#import "JSMNewSaveViewController.h"
#import "JSMHeaderCell.h"
#import "JSMDetailCell.h"

@interface JSMNewSaveViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, JSMDetailCellDelegate>
@property(nonatomic, strong)BaseCollectionView *headercollection;
@property(nonatomic, strong)BaseCollectionView *detailcollection;
@property(nonatomic, strong)NSArray *titleArr;
@property(nonatomic, strong)NSMutableArray *touringArr;
@property(nonatomic, strong)NSMutableArray *leavingingArr;
@property(nonatomic, strong)NSMutableArray *dreamCitySpecialArr;
@property(nonatomic, strong)NSMutableArray *dreamCityPrefectureArr;
@property(nonatomic, strong)NSMutableArray *strategyArr;
@property(nonatomic, assign)NSInteger num_select;
@end
@implementation JSMNewSaveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"<<" style:UIBarButtonItemStylePlain target:self action:@selector(backAction)];
    [self createHeaderCollection];
    [self createDetailCollection];
}
- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark 获取收藏数据
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.titleArr = @[LanguageTool(@"启程"), LanguageTool(@"梦"), LanguageTool(@"都市"), LanguageTool(@"一起向")];
    self.touringArr = [SaveTool allSaveModelInPlisttype:SaveTouringModel];
    self.leavingingArr = [SaveTool allSaveModelInPlisttype:SaveLeavingModel];
    self.dreamCitySpecialArr = [SaveTool allSaveModelInPlisttype:SaveDreamCitySpecialModel];
    self.dreamCityPrefectureArr = [SaveTool allSaveModelInPlisttype:SaveDreamCityPrefectureModel];
    [self.detailcollection reloadData];

}
#pragma mark 创建视图
- (void)createHeaderCollection{
    UICollectionViewFlowLayout *headerFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    headerFlowLayout.itemSize = CGSizeMake(75, 30);
    headerFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.headercollection = [[BaseCollectionView alloc]initWithFrame:CGRectMake(60, 20, VIEWWIDTH-60, 44) collectionViewLayout:headerFlowLayout];
    self.headercollection.backgroundColor = [UIColor clearColor];
    self.navigationItem.titleView = self.headercollection;
    self.headercollection.showsHorizontalScrollIndicator = NO;
    self.headercollection.delegate = self;
    self.headercollection.dataSource = self;
    [self.headercollection registerClass:[JSMHeaderCell class] forCellWithReuseIdentifier:@"header"];
}

- (void)createDetailCollection{
    UICollectionViewFlowLayout *detailFlowLayout = [[UICollectionViewFlowLayout alloc]init];
    detailFlowLayout.itemSize = CGSizeMake(VIEWWIDTH, VIEWHEIGHT - 64);
    detailFlowLayout.minimumLineSpacing = 0;
    detailFlowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.detailcollection = [[BaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, VIEWWIDTH, VIEWHEIGHT) collectionViewLayout:detailFlowLayout];
    self.detailcollection.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.detailcollection];
    self.detailcollection.showsHorizontalScrollIndicator = NO;
    self.detailcollection.delegate = self;
    self.detailcollection.dataSource = self;
    self.detailcollection.pagingEnabled = YES;
    [self.detailcollection registerClass:[JSMDetailCell class] forCellWithReuseIdentifier:@"detail"];
    
    
}
#pragma mark 协议方法
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.titleArr.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    if ([collectionView isEqual: self.headercollection]) {
        JSMHeaderCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"header" forIndexPath:indexPath];
        cell.myLable.text = self.titleArr[indexPath.item];
        if (indexPath.row == self.num_select) {
            cell.lineLable.hidden = NO;
            cell.myLable.textColor = [UIColor blueColor];
        }else{
            cell.myLable.textColor = [UIColor blackColor];
            cell.lineLable.hidden = YES;
        }
        return cell;
    }else{
        JSMDetailCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"detail" forIndexPath:indexPath];
        cell.num_item = indexPath.item;
        cell.myDelegate = self;
        if (indexPath.item == 3) {
            cell.arr = self.touringArr;
        }else if (indexPath.item == 0){
            cell.arr = self.leavingingArr;
        }else if (indexPath.item == 1){
            cell.arr = self.dreamCitySpecialArr;
        }else if (indexPath.item == 2){
            cell.arr = self.dreamCityPrefectureArr;
        }else if (indexPath.item == 4){
            cell.arr = self.strategyArr;
        }
        return cell;
    }
}
#pragma mark 点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView == self.headercollection) {
        //头部
        self.detailcollection.contentOffset =  CGPointMake(VIEWWIDTH * indexPath.row, 0);
        self.num_select = indexPath.row;
        [self.headercollection reloadData];
    }
}

#pragma mark 滑动的协议方法
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger num = self.detailcollection.contentOffset.x / VIEWWIDTH;
    if (scrollView == self.detailcollection) {
        self.headercollection.contentOffset = CGPointMake(134 / 7 * num, 0);
        self.num_select = num;
        [self.headercollection reloadData];
    }
    
}

#pragma mark 协议方法
- (void)pushVC:(BaseViewController *)push_viewControll{
    [self.navigationController pushViewController:push_viewControll animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
